#include <QMutex>
#include "MessageServices.h"

namespace Domy {

MessageServices::MessageServices(QObject *parent)
    : QObject(parent)
{

}

MessageServices::~MessageServices()
{

}

MessageServices *MessageServices::getServices()
{
    static MessageServices *s_services = Q_NULLPTR;
    static QMutex m_mutex;
    QMutexLocker lock(&m_mutex);
    if (!s_services) {
        s_services = new MessageServices();
    }
    return s_services;
}

void MessageServices::showStatus(const QString &text, int timeout)
{
    emit signalShowStatus(text, timeout);
}

void MessageServices::appendMessage(MessageType type, const QString &msg)
{
    emit signalAppendMessage(type, QStringList() << msg);
}

void MessageServices::appendMessages(MessageType type, const QStringList &msg_list)
{
    emit signalAppendMessage(type, msg_list);
}

void MessageServices::appendIssues(const Issues &issues)
{
    MessageType msg_type = (issues.type == IssuesType_Error) ? MsgType_Error : MsgType_Warning;

    QString msg = QString("[file: %1 line: %2]: %3 [%4]")
            .arg(issues.file_name).arg(issues.line).arg(issues.text).arg(issues.time);

    appendMessage(msg_type, msg);

    emit signalAppendIssues(issues);
}

} // namespace Domy
